package com.lysj.food.food.service;

import com.lysj.admin.utils.exception.BaseException;
import com.lysj.admin.utils.spring.foundation.base.BaseService;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.admin.utils.web.SelectItem;
import com.lysj.food.food.domain.FoodAttribute;
import com.lysj.food.food.domain.FoodAttributeDetail;
import com.lysj.food.food.repository.FoodAttributeDetailRepository;
import com.lysj.food.food.repository.FoodAttributeRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.*;

/**
 * @author Created by wtl on 2019-03-19 16:00:45
 * @description 菜品属性服务层
 */
@Slf4j
@Service
@Transactional
public class FoodAttributeService implements BaseService<FoodAttribute> {

    @Resource
    private FoodAttributeRepository foodAttributeRepository;

    @Resource
    private FoodAttributeDetailRepository foodAttributeDetailRepository;

    @Override
    public FoodAttributeRepository getRepository() {
        return foodAttributeRepository;
    }


    /**
     * @param storeId 门店id
     * @author Created by wtl on 2019/3/19 18:50
     * @Description 获取菜品属性列表
     */
    public Map<String, Object> findFoodAttrList(String storeId, String foodAttributeId) {
        // 获取菜品属性，所有门店都一样，暂时4种
        List<FoodAttribute> foodAttributes = foodAttributeRepository.findAll();
        // 没有菜品属性则创建4个属性
        if (foodAttributes.isEmpty()) {
            foodAttributes.add(new FoodAttribute("做法",1));
            foodAttributes.add(new FoodAttribute("口味",2));
            foodAttributes.add(new FoodAttribute("规格",3));
            foodAttributes.add(new FoodAttribute("称量",4));
            foodAttributeRepository.save(foodAttributes);
        }
        if (ParamUtil.isBlank(foodAttributeId)) {
            // 默认菜品属性选的是第一个"做法"
            foodAttributeId = foodAttributes.get(0).getId();
        }

        // 根据菜品属性id从菜品详情表查找对应的详情
        List<FoodAttributeDetail> foodAttributeDetails = foodAttributeDetailRepository.findByFoodAttributeIdAndStoreId(foodAttributeId, storeId);
        Map<String, Object> map = new HashMap<>();
        map.put("tabItem", foodAttributes);
        map.put("detail", foodAttributeDetails);
        return map;
    }

    /**
     * @author Created by wtl on 2019/4/3 16:46
     * @Description 根据门店id获取菜品属性列表
     */
    public Map<String,Object> findStoreFoodAttribute(String storeId) {
        if (ParamUtil.isBlank(storeId)) {
            throw new BaseException("参数错误");
        }
        Map<String,Object> map = new HashMap<>();
        // 获取做法列表
        map.put("zuoFaItems",foodAttributeRepository.findStoreFoodAttribute(storeId,1));
        // 获取口味列表
        map.put("kouWeiItems",foodAttributeRepository.findStoreFoodAttribute(storeId,2));
        // 获取规格列表
        map.put("guiGeItems",foodAttributeRepository.findStoreFoodAttribute(storeId,3));
        // 获取称重列表
        map.put("chengZhongItems",foodAttributeRepository.findStoreFoodAttribute(storeId,4));
        //获取单位列表
        map.put("unitItems",foodAttributeRepository.findStoreFoodAttribute(storeId,5));
        return map;
    }

}